package cn.mw.cmdb.template.entity;

import cn.mw.cmdb.template.entity.convert.MwDiscoverRuleConvertDto;
import cn.mw.cmdb.template.entity.convert.MwGraphConvertDto;
import cn.mw.cmdb.template.entity.convert.MwMonitorItemConvertDto;
import cn.mw.cmdb.template.entity.convert.MwTriggerConvertDto;
import cn.mw.cmdb.template.enums.ZabbixMonitorTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

/**
 * @description 发现规则实体
 */
@Getter
@Setter
@Schema(name = "DiscoveryRuleDto", description = "发现规则实体")
public class DiscoveryRuleDto {

    @Schema(description = "发现规则ID")
    private String itemId;

    @Schema(description = "发现规则名称")
    private String name;

    @Schema(description = "发现规则键值")
    private String keyValue;

    @Schema(description = "发现规则间隔")
    private String delay;

    @Schema(description = "发现规则类型")
    private String type;

    @Schema(description = "发现规则状态")
    private String status;

    @Schema(description = "发现规则信息")
    private String description;

    @Schema(description = "监控项原型")
    private List<MwMonitorItemConvertDto> monitorItemPrototype;

    @Schema(description = "触发器原型")
    private List<MwTriggerConvertDto> triggerPrototype;

    @Schema(description = "图形原型")
    private List<MwGraphConvertDto> graphPrototype;

    public void extractFrom(MwDiscoverRuleConvertDto ruleConvertDto){
        this.itemId = ruleConvertDto.getItemid();
        this.name = ruleConvertDto.getName();
        this.keyValue = ruleConvertDto.getKey_();
        this.delay = ruleConvertDto.getDelay();
        ZabbixMonitorTypeEnum monitorTypeEnum = ZabbixMonitorTypeEnum.getMonitorTypeEnum(ruleConvertDto.getType());
        if(monitorTypeEnum != null){
            this.type = monitorTypeEnum.getName();
        }
        this.status = ruleConvertDto.getStatus();
        this.description = ruleConvertDto.getDescription();
        List<MwMonitorItemConvertDto> items = ruleConvertDto.getItems();
        if(CollectionUtils.isNotEmpty(items)){
            for (MwMonitorItemConvertDto item : items) {
                ZabbixMonitorTypeEnum monitorItemTypeEnum = ZabbixMonitorTypeEnum.getMonitorTypeEnum(item.getType());
                if(monitorItemTypeEnum != null){
                    item.setType(monitorItemTypeEnum.getName());
                }
            }
        }
        this.monitorItemPrototype = ruleConvertDto.getItems();
        this.triggerPrototype = ruleConvertDto.getTriggers();
        this.graphPrototype = ruleConvertDto.getGraphs();
    }
}
